package dpss.replicamanager.gui;

import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.net.DatagramSocket;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;

import dpss.commons.CustomLogger;
import dpss.replicamanager.data.ServerNodes;

/**
 * THE GUI application
 * 
 * @author musab.mirza
 * 
 */
public class GUI {

	private static CustomLogger logger = CustomLogger.getInstance();

	private NodesTableModel tableModel;
	private JTable listingTable;
	private ServerNodePopup serverNodePopup;
	private ServerNodes nodes;

	/**
	 * The GUI constructor
	 * 
	 * @param nodes
	 */
	public GUI(ServerNodes nodes) {
		tableModel = new NodesTableModel(nodes.getDataVector());
		serverNodePopup = new ServerNodePopup();
		this.nodes = nodes;
		setupframe();
	}

	/**
	 * Sets up the Frame and all the elements
	 */
	private void setupframe() {
		JFrame guiFrame = new JFrame();
		guiFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

		// make sure the program exits when the frame closes
		guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		guiFrame.setTitle("REPLICA MANAGER");
		guiFrame.setSize(600, 500);

		// This will center the JFrame in the middle of the screen
		guiFrame.setLocationRelativeTo(null);

		// Listing panel
		final JPanel nodesListingPanel = new JPanel();
		nodesListingPanel.add(new JLabel(
				"List of all connected server nodes nodes"));
		listingTable = new JTable(tableModel);
		listingTable.setFillsViewportHeight(true);
		listingTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
		listingTable.setDefaultRenderer(Object.class, new MyCellRenderer());

		listingTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
				rightClickActionHandler(e);
			}
		});

		JScrollPane scrollPane = new JScrollPane(listingTable);
		nodesListingPanel.add(scrollPane);

		guiFrame.add(nodesListingPanel, BorderLayout.CENTER);
		guiFrame.setVisible(true);
	}

	/**
	 * Right click action handler that shows the initialize and uninitialize
	 * menu
	 * 
	 * @param e
	 */
	private void rightClickActionHandler(MouseEvent e) {

		int r = listingTable.rowAtPoint(e.getPoint());
		int c = listingTable.columnAtPoint(e.getPoint());

		if (r >= 0 && c >= 0) {
			serverNodePopup.doPopup(tableModel.getData().get(r), e);
		}
	}

	/**
	 * Refreshes the table model with latest nodes data
	 */
	@SuppressWarnings("unchecked")
	public void refresh() {
		tableModel.setData(nodes.getDataVector());
		tableModel.fireTableDataChanged();
	}

	/**
	 * Gets the server node popup
	 * 
	 * @return
	 */
	public ServerNodePopup getServerNodePopup() {
		return serverNodePopup;
	}

	/**
	 * Shows an error popup
	 * 
	 * @param string
	 */
	public static void showError(String message) {
		JOptionPane.showMessageDialog(new JPanel(), message, "Error",
				JOptionPane.ERROR_MESSAGE);
	}

}
